<template>
<div class="app-container calendar-list-container editUser">
    <el-form :model="updatePwdForm" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="用户名" prop="username" >
        <el-input v-model="updatePwdForm.username"  placeholder="请输入账户" readonly ></el-input>
      </el-form-item>
      <el-form-item label="原密码" placeholder="请输入原密码" prop="password" >
        <el-input type="password" v-model="updatePwdForm.password" ></el-input>
      </el-form-item>
      <el-form-item label="新密码" placeholder="请输入新密码" prop="password" >
        <el-input type="password" v-model="updatePwdForm.newPassword" ></el-input>
      </el-form-item>
      <el-form-item label="新密码" placeholder="请再次输入新密码" prop="password" >
        <el-input type="password" v-model="updatePwdForm.newPassword1" ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="uedit-footer">
      <el-button @click="cancelPwd('updatePwdForm')">取 消</el-button>
      <el-button type="primary" @click="updateUserPassword('updatePwdForm')">确 定</el-button>
    </div>
</div>
</template>

<script>
import { updateUserPwd } from '@/api/admin/user/index'
import { mapGetters } from 'vuex'
import {
  Message
} from 'element-ui'
export default {
  name: 'user',
  data() {
    console.log(this.$store.state.user.userId)
    return {
      updatePwdForm: {
        id: undefined,
        username: this.$store.state.user.username,
        password: undefined,
        newPassword: undefined,
        newPassword1: undefined,
        userId: this.$store.state.user.userId
      },
      rules: {
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 20,
            message: '长度在 5 到 20 个字符',
            trigger: 'blur'
          }
        ]
      }

    }
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'userType',
      'platformId'
    ])
  },
  methods: {
    cancelPwd(formName) {
      this.dialogUpdatePwdVisible = false
      this.resetTemp()
    },
    updateUserPassword(formName) {
      if (this.updatePwdForm.password === this.updatePwdForm.newPassword) {
        Message({
          message: '新旧密码不能一致',
          type: 'warning'
        })
        return
      } else if (this.updatePwdForm.newPassword !== this.updatePwdForm.newPassword1) {
        Message({
          message: '新密码不一致',
          type: 'warning'
        })
        return
      }
      updateUserPwd(this.updatePwdForm)
        .then(response => {
          this.dialogUpdatePwdVisible = false
          if (response.code === '0000') {
            this.getList()
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          } else {
            this.$notify({
              title: '失败',
              message: response.msg,
              type: 'success',
              duration: 2000
            })
          }
        })
    }

  }
}
</script>
<style>
.editUser .el-input__inner {
  width: 400px !important;
}
.uedit-footer{
  margin-left: 100px;
}
</style>
